<template>
  <div class="newList">
    <div class="newList-title">
      <span class="newList-title-p1 reset">{{title}}</span>
    </div>
    <van-list
      v-if="listAll.length>0"
      v-model="loading"
      finished-text="No more"
      :finished="finished"
      @load="onLoad"
      class="news-box"
    >
      <van-cell class="news-item" v-for="(i,index) in list" :key="index">
        <newsItem :i="i"></newsItem>
      </van-cell>
    </van-list>
  </div>
</template>

<script type="text/ecmascript-6">
import { getmorereportdata } from "../../api/sort";
import newsItem from "./newstLi";
export default {
  components: {
    newsItem
  },
  data() {
    return {
      title:this.$route.query.title,
      list: [],
      finished: false,
      finishedText: "",
      listAll: [], //所有新闻，规定只给50条，当所有新闻,自己做分页功能
      error: false,
      loading: false,
      page: 0,
      pageSize: 10,
      isFirstEnter: false
    };
  },
  methods: {
    onLoad() {
      // 异步更新数据
      setTimeout(() => {
        let begin = this.page * this.pageSize;
        let end = (this.page + 1) * this.pageSize;
        for (let i = begin; i < end; i++) {
          if (this.listAll[i]) {
             this.list.push(this.listAll[i]);
          }
        }
        this.page++;
        // 加载状态结束
        this.loading = false;
        // 数据全部加载完成
        if (this.list.length >= this.listAll.length) {
          this.finished = true;
          if (this.listAll.length > 4) {
            this.finishedText = "No more";
          }
        }
      }, 500);
    },
    //获取新闻信息
    getchannelist() {
      let obj = this.$route.query || {};
      if (this.limit) {
        obj.limit = this.limit;
      }
      getmorereportdata(obj).then(res => {
        if (res.code === "0000") {
          this.listAll = res.data || [];
        }
      });
    }
  },
  created() {
    this.isFirstEnter = true;
  },
  activated() {
    if (!this.$route.meta.isBack || this.isFirstEnter) {
  
   
      // 如果isBack是false，表明需要获取新数据，否则就不再请求，直接使用缓存的数据
      // 如果isFirstEnter是true，表明是第一次进入此页面或用户刷新了页面，需获取新数据
      // 把数据清空，可以稍微避免让用户看到之前缓存的数据
       this.title=this.$route.query.title,
         this.list= [],
         this.finished= false,
        this.finishedText= "",
        this.listAll= [], //所有新闻，规定只给50条，当所有新闻,自己做分页功能
        this.error=false,
        this.loading= false,
        this.page= 0,
        this.pageSize= 10,
            this.isFirstEnter= false
      this.getchannelist();
    }
    // 恢复成默认的false，避免isBack一直是true，导致下次无法获取数据
    this.$route.meta.isBack = false;
    // 恢复成默认的false，避免isBack一直是true，导致每次都获取新数据
    this.isFirstEnter = false;
  }
};
</script>

<style scoped lang="less">
@import url("./news.less");
</style>
